package com.yuexunit.operationman.nbsy.module.SurgicalReceipt.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.Bean.SurgicalReceiptBean;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.SurgicalReceiptContracts;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.model.impl.SurgicalReceiptDetailModel;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.presenter.ISurgicalReceiptDetailPresenter;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.presenter.impl.SurgicalReceiptDetailPresenter;
import com.yuexunit.operationman.nbsy.module.SurgicalReceipt.ui.view.ISurgicalReceiptDetailView;
import com.yuexunit.operationman.nbsy.util.ProUtil;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/25 0025.
 */

public class Act_SurgicalReceipt_Main extends BaseKJActivity implements ISurgicalReceiptDetailView {

    @Bind(R.id.titlebar_tv_title)
    TextView titlebarTvTitle;
    @Bind(R.id.pic)
    ImageView pic;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.age)
    TextView age;
    @Bind(R.id.sex)
    TextView sex;
    @Bind(R.id.patient)
    RelativeLayout patient;
    @Bind(R.id.position)
    TextView position;
    @Bind(R.id.admission_number)
    TextView admissionNumber;
    @Bind(R.id.bed_number)
    TextView bedNumber;
    @Bind(R.id.diagnose)
    TextView diagnose;

    ISurgicalReceiptDetailPresenter presenter;

    LoadingDialog loadingDialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_surgicalreceipt_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new SurgicalReceiptDetailPresenter(this, new SurgicalReceiptDetailModel());
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
        presenter.getSurgicalReceiptDate(params);
    }

    private void setTitle() {
        titlebarTvTitle.setText("手术交接单");
    }


    @OnClick({R.id.titlebar_img_back, R.id.surgicalreceipt_01, R.id.surgicalreceipt_02, R.id.surgicalreceipt_03, R.id.surgicalreceipt_04, R.id.preview})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.surgicalreceipt_01:
                startActivity(new Intent(aty, Act_SurgicalReceipt_Ward.class));
                break;
            case R.id.surgicalreceipt_02:
                startActivity(new Intent(aty, Act_SurgicalReceipt_Oprate.class));
                break;
            case R.id.surgicalreceipt_03:
                startActivity(new Intent(aty, Act_SurgicalReceipt_Postoperative.class));
                break;
            case R.id.surgicalreceipt_04:
                startActivity(new Intent(aty, Act_SurgicalReceipt_Sign.class));
                break;
            case R.id.preview:
                startActivity(new Intent(aty, Act_SurgicalReceipt_Preview.class));
                break;
        }
    }

    @Override
    public void getSurgicalReceiptDate(SurgicalReceiptBean surgicalReceiptBean) {
//        if (true) {
        if (AppConfig.isEye && null == surgicalReceiptBean.getObject().getTransferHandover().getHandoverId()) {
            String safe = ProUtil.getJson("SurgicalReceipt.json",aty);
            SurgicalReceiptBean surgicalReceiptBean1= JSON.parseObject(safe,SurgicalReceiptBean.class);
            if (surgicalReceiptBean.getCode() == 200) {
                surgicalReceiptBean1.getObject().setTransferHandover(surgicalReceiptBean.getObject().getTransferHandover());
                AppConfig.SURGICALRECEIPTBEAN = surgicalReceiptBean1;

                name.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientName());
                age.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientAge());
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("1")) {
                    sex.setText("男");
                } else if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("2")) {
                    sex.setText("女");
                } else {
                    sex.setText("不详");
                }
                position.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getDepartmentName());
                admissionNumber.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getInHospitalNo());
                bedNumber.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBedNo());
                diagnose.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getDiagnosis());
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getHandoverItemList() == null) {
                    AppConfig.SURGICALRECEIPTBEAN.getObject().setHandoverItemList(SurgicalReceiptContracts.handoverItemListEntities_New(this));
                }
            }
        } else {
            if (surgicalReceiptBean.getCode() == 200) {
                AppConfig.SURGICALRECEIPTBEAN = surgicalReceiptBean;
                name.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientName());
                age.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientAge());
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("1")) {
                    sex.setText("男");
                } else if (AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getPatientSex().equals("2")) {
                    sex.setText("女");
                } else {
                    sex.setText("不详");
                }
                position.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getDepartmentName());
                admissionNumber.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getInHospitalNo());
                bedNumber.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getBedNo());
                diagnose.setText(AppConfig.SURGICALRECEIPTBEAN.getObject().getTransferHandover().getDiagnosis());
                if (AppConfig.SURGICALRECEIPTBEAN.getObject().getHandoverItemList() == null) {
                    AppConfig.SURGICALRECEIPTBEAN.getObject().setHandoverItemList(SurgicalReceiptContracts.handoverItemListEntities_New(this));
                }
            }
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
